import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

let router=new Router({
  //滚动条
  scrollBehavior(to, from, savedPosition) {
    // 始终滚动到顶部
    return { y: 0 }
  },
  routes: [
    {
      path: '/login',
      component: ()=>import('@/pages/Login/Login')
    },
    {
      path: '/home',
      component: ()=>import('@/components/Home'),
      redirect:'/info',
      children:[
        {
          path:'/info',
          component:()=>import('@/pages/Home/Info')
        },
        {
          path:'/consumer',
          component:()=>import('@/pages/Home/Consumer')
        },
        {
          path:'/singer',
          component:()=>import('@/pages/Home/Singer')
        },
        {
          path:'/song',
          component:()=>import('@/pages/Home/Song')
        },
        {
          path:'/songlist',
          component:()=>import('@/pages/Home/SongList')
        },
        {
          name:'listSong',
          path:'/listsong',
          component:()=>import('@/pages/Home/ListSong')
        },
        {
          name:'remake',
          path:'/remake',
          component:()=>import('@/pages/Home/Remake')
        }
      ]
    },
    {
      path:'*',
      redirect:'/login'
    }
  ]
})
//配置全局前置守卫
router.beforeEach( (to,from,next)=>{
  //to:跳转到哪个地方,from:来自哪个地方，next：放行函数 next(path)放行到指定路由
  //获取uuid
  let uuid=localStorage.getItem('uuid')
  if (uuid){
      if (to.path=='/login'){
        next('/home')
      }
      next()
  }else {
    if (to.path=='/home'||to.path=='/info'||to.path=='/consumer'||to.path=='/singer'||to.path=='/song'||to.path=='/songlist'||to.path=='/listsong'||to.path=='/remake'){
      next('/login')
    }
    next()
  }
})
export default router
